<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

/**
 * 用于处理保存变量的文件类，使用方法：
 	1，加载文件：
 		$cls_variable=new cls_variable();
 		$cls_variable->set_file('d:/','2.txt');
   		$cls_variable->load();
   	2，保存一个变量到文件里：
 		$cls_variable=new cls_variable();
 		$cls_variable->set_file('d:/','2.txt');   		
   		$cls_variable->save($v_name,$v_value);
   	3, 保存多个变量到文件里：
   		$cls_variable->load();
   		foreach($kerchief as $key=>$value)
		{
			$cls_variable->add($key,$value);
		}
		$cls_variable->save_all();
	4，获取一个变量值：
		$cls_variable->get($v_name);
	5, 获取所有变量：
		$cls_variable->get();
	6, 清除文件里保存的所有变量
		$cls_variable->clear_file();
	7，只清除变量类里的变量
		$cls_variable->clear();
	8, 清除文件里保存的某个变量
		$cls_variable->clear_file($v_name);
	9，只清除变量类里的变量
		$cls_variable->clear($v_name);		
 */

require_once(dirname(__FILE__) . '/cls_file.php');
require_once(dirname(__FILE__) . '/cls_array.php');

class cls_variable extends cls_file{
	
	private $path='';
	private $file='';
	private $separator="\r\n";
	
	public $variables=array();
	
	public function cls_variable($path='',$file='',$coding='utf-8')
	{
		$this->coding=$coding;
		
		$this->set_file($path,$file);
	}
	
	//public function __construct($path='',$file='',$coding='utf-8')
	//{
	//	$this->cls_variable($path,$file,$coding);
	//}

	public function set_file($path='',$file='')
	{
		if(!empty($path) && !empty($file))
		{
			$path=$this->transact_path($path);
			
			if(!file_exists($path))
			{
				if($this->create_dir($path))
				{
					$this->path=$path;
					$this->file=$file;					
				}
				else{
					return false;
				}
			}
			else{
				$this->path=$path;
				$this->file=$file;				
			}
		}
		else{
			return false;
		}
		
		if(file_exists($path.$file)  && !is_writable($path.$file))//文件存在但是不可写
		{
			return false;
		}
		else{
			return true;
		}
	}
	
	
	/*
	 * 从文件加载变量
	 */
	public function load()
	{
		$this->clear();	

		if(!file_exists($this->path.$this->file))
		{
			return false;
		}
		
		$content=$this->trim_separator($this->read_file($this->path,$this->file));
		
		if($content==false)
		{
			return false;
		}
		else{
			$content_array=explode($this->separator,$content);
			
			$cls_array=new cls_array();
			
			if(!empty($content_array))
			{
				foreach($content_array as $key=>$row)
				{
					$row=preg_replace("/\s+|\s+/",'|',$row);
					$variable_info=explode('|',$row);
					
					$this->variables[trim($variable_info[0])]=$cls_array->decode(unserialize(trim($variable_info[1])));
				}
			}
			
			return $this->get();
		}
	}
	
	/*
	 *  保存变量到文件里
	 */
	public function save($v_name,$v_value)
	{		
		$this->load();
		
		if($v_name==='')
		{
			$v_name=count($this->variables);
		}
		
		//判断变量是否已经在文件里
		if(!isset($this->variables[$v_name]))//不存在
		{
			$this->variables[$v_name]=$v_value;
	
			$cls_array=new cls_array();
			$row=$this->separator.trim($v_name)."|".serialize($cls_array->encode($v_value));

			$this->write_file($this->path,$this->file,$row,'append');			
		}
		else{//存在
			$this->variables[$v_name]=$v_value;	
			
			$this->save_all($this->path,$this->file);
		}

		return $this->get();
	}
	
	/*
	 * 添加变量
	 */
	public function add($v_name='',$v_value='')
	{
		if($v_name==='')
		{
			$v_name=count($this->variables);
		}
		
		$this->variables[$v_name]=$v_value;

		return $this->get();		
	}
	
	/*
	 * 保存所有变量到文件里
	 */
	public function save_all()
	{
		$cls_array=new cls_array();
		$variables=$cls_array->encode($this->variables);		
		
		if(!empty($variables))
		{
			foreach($variables as $v_name => $v_value)
			{
					$row.=$this->separator.trim($v_name)."|".serialize($v_value);
			}
			
		}		
	
		$this->write_file($this->path,$this->file,$row,'rewrite');	
	}
	
	
	private function trim_separator($str)
	{
		$sign_len=mb_strlen($this->separator,$this->coding);
		$str_len=mb_strlen($str,$this->coding);
		
		if(mb_substr($str,0,$sign_len,$this->coding)==$this->separator)
		{
			$str=mb_substr($str,$sign_len,$str_len-$sign_len);
		}

		if(mb_substr($str,0-$sign_len,$sign_len,$this->coding)==$this->separator)
		{
			$str=mb_substr($str,0,$str_len-$sign_len);
		}		
		
		return $str;
	}
	
	/*
	 * 清除变量
	 */
	public function clear($v_name='')
	{
		if($v_name==='')//清除所有变量
		{
			$this->variables=array();
		}	
		else{
			unset($this->variables[$v_name]);
		}
	}
	
	/*
	 * 清除变量并清除文件
	 */	
	public function clear_file($v_name='')
	{	
		$this->load();
	
		if($v_name==='')//清除所有变量
		{
			$this->clear();
			
			if(file_exists($this->path.$this->file))
			{
				return unlink($this->path.$this->file);
			}							 
		}	
		else{
			$this->clear($v_name);
			$this->save_all();
		}
	}
	
	/*
	 * 获取某个变量
	 */	
	public function get($v_name='')
	{
		if($v_name!=='')
		{
			return $this->variables[$v_name];
		}
		else{
			return $this->variables;
		}
	}

}

?>